puts "Script Executer Utility"
puts "-----------------------"

$error=false
$errname=""
$errlin=0

def load(script)
  s=File.new(script)
  sc=""
  s.each { |line|
    sc+=line+"\n"
  }
  eval("begin\n"+sc+"\nrescue\n$error=true\n$errname=$!\n$errlin=$@\nend")
  if $error    
    $error=false
    throw
  end
end


if ARGV[0]
  if File.file?(ARGV[0])
    begin
      load ARGV[0]
      puts
      puts "Execution done."
      puts "Press ENTER to continue..."
      STDIN.getc
      exit
    rescue
      puts
      puts "SCRIPT ERROR:"
      puts $errlin
      puts
      puts $errname
      puts
      puts "Press ENTER to continue..."
      STDIN.getc
      exit
    end
  else
    puts "Bad file. Please enter filename manually."
  end
else
  puts "Enter script location:"
end

puts

cont=true

while cont
  print "Filename or 'exit': "
  script=gets.strip
  if script.downcase=="exit"
    exit
  end
  if File.file?(script)
    cont=false
  else
    puts "Error: Specified file does not exist."
  end
end

begin
  load script
  puts
  puts "Execution done."
  puts "Press ENTER to continue..."
  STDIN.getc
  exit
rescue
  puts
  puts "SCRIPT ERROR:"
  puts $errlin
  puts
  puts $errname
  puts
  puts "Press ENTER to continue..."
  STDIN.getc
  exit
end